import { defineStore } from 'pinia'

// import type { ILoginInfo } from '@/types/login'

import { getUserInfoByToken } from '@/api/login'

import type { AxiosResponse } from 'axios'

type Token = {
  token: string
  tokenExpired: string
}

type UserInfo = { nickname: string; avatar: string }

type User = {
  permission: any[]
  uid: string
  userInfo: object
  role: string[]
}

export const useUserStore = defineStore('user', {
  state: () => ({
    token: '',
    tokenExpired: '',
    role: [],
    permission: [],
    uid: '',
    userInfo: {
      avatar: '',
      nickname: ''
    }
  }),
  actions: {
    getUserInfo() {
      getUserInfoByToken().then((res: any) => {
        console.log(res)

        let { permission, uid, userInfo, role } = res

        this.permission = permission
        this.uid = uid
        this.userInfo = userInfo
        this.role = role
      })
    },
    saveToken({ token, tokenExpired }: Token) {
      this.token = token
      this.tokenExpired = tokenExpired
    }
  }
})
